package com.megvii.koala.tv.presenter;

import com.megvii.corelib.net.Callback;
import com.megvii.corelib.util.AppUtil;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.constance.ErrorCode;
import com.megvii.koala.tv.data.TVGlobalData;
import com.megvii.koala.tv.data.ThemeBean;
import com.megvii.koala.tv.data.ThemeManager;
import com.megvii.koala.tv.event.TVEventAPI;
import com.megvii.koala.tv.fragment.ThemeFragment;
import com.megvii.koala.tv.net.TVNetAPI;
import com.megvii.koala.tv.net.tv.GetThemeListResponse;

/**
 * Created by linyue on 16/1/25.
 */
public class ThemePresenter extends KoalaPresenter<ThemeFragment>
        implements ThemeFragment.ThemeListener
{
    private ThemeManager themeManager;

    @Override
    protected void onCreated()
    {
        super.onCreated();
        themeManager = TVGlobalData.instance().themeManager;
        viewHolder.setListener(this);
        String deviceToken = AppUtil.getDeviceUUID(viewHolder.getActivity());
        TVNetAPI.requestGetThemeList(deviceToken, this, this);
    }

    @Callback(clazz = GetThemeListResponse.class)
    public void onGetThemeListResponse(GetThemeListResponse response)
    {
        if (response.code == ErrorCode.NO_ERROR)
        {
            themeManager.update(response.data.theme_config);
            viewHolder.updateView(themeManager.getAll());
        }
        else
        {
            showToast(ErrorCode.getDescription(response.code));
        }
    }

    @Override
    public void onBackPressed()
    {
        viewHolder.finish();
    }

    @Override
    public void onChooseTheme(ThemeBean theme)
    {
        TVEventAPI.sendChooseTheme(theme.theme);
        viewHolder.finishFragments();
    }
}
